package com.luhuiguo.cms.server.handler;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import com.luhuiguo.cms.server.util.MapperUtils;
import com.luhuiguo.cms.server.util.SecurityUtils;
import com.luhuiguo.cms.shared.model.UserModel;
import com.luhuiguo.cms.shared.rpc.Logout;
import com.luhuiguo.cms.shared.rpc.LogoutResult;
import com.luhuiguo.gwt.dispatcher.server.ActionHandler;
import com.luhuiguo.gwt.dispatcher.server.ExecutionContext;
import com.luhuiguo.gwt.dispatcher.shared.exception.ActionException;

@Component
public class LogoutHandler implements ActionHandler<Logout, LogoutResult> {


    @Override
    public LogoutResult execute(Logout action, ExecutionContext context) throws ActionException {

        UserModel model = MapperUtils.map(SecurityUtils.getCurrentUser(), UserModel.class);

        SecurityContextHolder.clearContext();

        return new LogoutResult(model);
    }

    @Override
    public Class<Logout> getActionType() {
        return Logout.class;
    }

    @Override
    public void rollback(Logout action, LogoutResult result, ExecutionContext context) throws ActionException {

    }

}
